package com.sm.controller;


import cn.hutool.core.util.ObjectUtil;
import com.sm.model.UserBankCard;
import com.sm.service.UserBankCardService;
import com.sm.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 用户银行卡
 */
@Controller
@RequestMapping("/userBankCard")
public class UserBankCardController {
    @Autowired
    private UserBankCardService userBankCardService;

    /**
     * 查询用户银行卡详情
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectAll")
    public Msg selectAll(@RequestParam("userId") Integer userId) {
        UserBankCard userBankCard = userBankCardService.selectAll(userId);
        return Msg.success().add("userBankCard", userBankCard);
    }

    /**
     * 更改银行卡
     *
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateById")
    public Msg updateById(UserBankCard userBankCard) {
        int i = userBankCardService.updateById(userBankCard);
        if (i == 1) {
            return Msg.success().add("msg", "更改成功");
        }
        return Msg.fail();
    }

    /**
     * 添加银行卡
     *
     * @param userBankCard
     * @return
     */
    @ResponseBody
    @RequestMapping("/insertBank")
    public Msg insertBank(UserBankCard userBankCard) {
        UserBankCard bankCard = userBankCardService.selectAll(userBankCard.getUserId());
        if (ObjectUtil.isNotEmpty(bankCard)) {
            userBankCard.setBankCardId(bankCard.getBankCardId());
            int i = userBankCardService.updateById(userBankCard);
            if (i == 1) {
                return Msg.success().add("msg", "更改成功");
            }

        }else{
            int a = userBankCardService.insertBank(userBankCard);
            if (a == 1) {
                return Msg.success().add("msg", "添加成功");
            }
        }


        return Msg.fail();
    }
}
